package tw.edu.tku.im.maze;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class RoomListRepParser extends PacketParser {
   public static RoomListRepParser INSTANCE = new RoomListRepParser();
   
   private RoomListRepParser() { }
   
   @Override
   public RoomListRep parse(JSONObject jsonObj) throws JSONException {
      if(jsonObj.isNull("result")) {
         return new RoomListRep();
      }
      else {
         JSONArray roomListJSONArray =  jsonObj.getJSONArray("result");
         Room[] list = new Room[roomListJSONArray.length()];
         for(int i=0; i<roomListJSONArray.length(); i++) {
            if(roomListJSONArray.isNull(i)) { break; }
            list[i] = Room.parse(roomListJSONArray.getJSONObject(i));
         }
         return new RoomListRep(list);
      }
   }

   @Override
   public String acceptName() {
      return "roomListResponse";
   }
   
}
